<?php


/**
* CakePHP Schema Plugin
* http://cake-schema-plugin.googlecode.com
*
* @package schema
* @subpackage schema.views.helpers
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: x_form.php 204 2010-11-19 09:07:33Z zetruger@gmail.com $
* @link $HeadURL: http://cake-schema-plugin.googlecode.com/svn/trunk/views/helpers/x_form.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


App::import('Helper', 'Form');


class XFormHelper extends FormHelper {

var $helpers = array(
	'Html',
	'Js'
);

var $map = array(
	'integer' => 'number',
	'float' => 'number'
);

var $_newTypes = array(
	'color',
	'email',
	'fixed',
	'number',
	'range',
	'search',
	'skip',
	'tel',
	'url',
	'week',
	'element',
	'textElement',
	'wysiwyg',
	'youtube'
);

function _getInputOptions($fieldName) {
	$this->setEntity($fieldName);
	$modelKey = $this->model();
	$fieldKey = $this->field();
	if (ClassRegistry::isKeySet($modelKey)) {
		$object = &ClassRegistry::getObject($modelKey);
		if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
			return $object->getInputOptions($fieldKey);
		}
	}
	return array();
}

function input($fieldName, $options = array()) {
	unset($options['_type']);
	$options = Set::merge($this->_getInputOptions($fieldName), $options);
	if (isset($options['type'])) {
		$type = $options['type'];
		if ($type == 'static') {
			$type = 'fixed';
		}
		if ($type == 'skip') {
			return NULL;
		} elseif (in_array($type, $this->_newTypes)) {
			$options['type'] = 'textarea';
			$options['_type'] = $type;
		}
	}
	$languages = (array)Configure::read('Config.languages');
	if (count($languages) > 1) {
		$output = '';
		foreach ($languages as $language) {
			//
		}
		//return $output;
	}
	return parent::input($fieldName, $options);
}

function color($fieldName, $options = array()) {
	$options['type'] = 'color';
	return parent::text($fieldName, $options);
}

function email($fieldName, $options = array()) {
	$options['type'] = 'email';
	return parent::text($fieldName, $options);
}

function fixed($fieldName, $options = array()) {
	$options = $this->_initInputField($fieldName, $options);
	$text = $options['value'];
	unset($options['name'], $options['value'], $options['label']);
	$options = $this->addClass($options, 'fixed');
	return $this->Html->tag('span', h($text), $options);
}

function number($fieldName, $options = array()) {
	$options['type'] = 'number';
	return parent::text($fieldName, $options);
}

function range($fieldName, $options = array()) {
	$options['type'] = 'range';
	return parent::text($fieldName, $options);
}

function search($fieldName, $options = array()) {
	$options['type'] = 'search';
	return parent::text($fieldName, $options);
}

function tel($fieldName, $options = array()) {
	$options['type'] = 'tel';
	return parent::text($fieldName, $options);
}

function url($fieldName, $options = array()) {
	if (is_string($fieldName) && is_array($options) && (strpos($fieldName, '/') === FALSE)) {
		$options['type'] = 'url';
		return parent::text($fieldName, $options);
	}
	return parent::url($fieldName, $options);
}

function week($fieldName, $options = array()) {
	$options['type'] = 'week';
	return parent::text($fieldName, $options);
}

function _renderElement($name, $params = array(), $loadHelpers = FALSE) {
	$view = &ClassRegistry::getObject('view');
	return $view->element($name, $params, $loadHelpers);
}

function element($fieldName, $options = array()) {
	if (isset($options['element'])) {
		$element = $options['element'];
		unset($options['element']);
	} else {
		$this->setEntity($fieldName);
		$element = $this->field();
	}
	return $this->_renderElement($element, array('fieldName' => $fieldName, 'options' => $this->_initInputField($fieldName, $options), 'plugin' => 'schema'));
}

function textElement($fieldName, $options = array()) {
	if (isset($options['element'])) {
		$element = $options['element'];
		unset($options['element']);
	} else {
		$this->setEntity($fieldName);
		$element = $this->field();
	}
	return parent::text($fieldName, $options) . $this->_renderElement($element, array('fieldName' => $fieldName, 'options' => $this->_initInputField($fieldName, $options), 'plugin' => 'schema'));
}

function file($fieldName, $options = array()) {
	$output = $this->_renderElement('file', array('fieldName' => $fieldName, 'options' => $this->_initInputField($fieldName, $options), 'plugin' => 'schema'));
	unset($options['filetype'], $options['config']);
	return parent::file($fieldName, $options) . $output;
}

function wysiwyg($fieldName, $options = array()) {
	if (isset($options['wysiwyg'])) {
		$wysiwyg = $options['wysiwyg'];
		unset($options['wysiwyg']);
} elseif (is_null($wysiwyg = Configure::read('Config.wysiwyg'))) {
if (defined('DEFAULT_WYSIWYG')) {
$wysiwyg = DEFAULT_WYSIWYG;
} else {
$wysiwyg = 'tinymce';
}
}
	$output = $this->_renderElement($wysiwyg, array('fieldName' => $fieldName, 'options' => $this->_initInputField($fieldName, $options), 'plugin' => 'schema'));
	unset($options['theme'], $options['config']);
	return parent::textarea($fieldName, $options) . $output;
}

function youtube($fieldName, $options = array()) {
	$output = $this->_renderElement('youtube', array('fieldName' => $fieldName, 'options' => $this->_initInputField($fieldName, $options), 'plugin' => 'schema'));
	unset($options['width'], $options['height']);
	return parent::text($fieldName, $options) . $output;
}

function textarea($fieldName, $options = array()) {
	if (isset($options['_type'])) {
		$type = $options['_type'];
		unset($options['_type']);
		if ($type == 'wysiwyg') {
			return $this->wysiwyg($fieldName, $options);
		} elseif (in_array($type, $this->_newTypes)) {
			unset($options['cols'], $options['rows']);
			return $this->{$type}($fieldName, $options);
		}
	}
	return parent::textarea($fieldName, $options);
}

function beforeRender() {
	parent::beforeRender();
	// helper injection
	$view = &ClassRegistry::getObject('view');
	if (isset($view->Form)) {
		$view->loaded['form'] = &$this;
		$view->Form = &$this;
	}
	foreach (array_keys($view->loaded) as $helper) {
		if (isset($view->loaded[$helper]->Form)) {
			$view->loaded[$helper]->Form = &$this;
		}
	}
}

}


?>